"""
@File    : OpExcel.py
@Time    : 2019/8/2 9:49
@Author  : LiuFeiYu
@Email   : liufeiyu@sunrise.net
@Software: PyCharm
"""
import pandas as pd
import os
import json

_data_path = os.path.dirname(os.path.dirname(os.path.abspath(
    __file__))) + '\\Data\\'


class ExcelData:
    def __init__(self, excel_file):
        self._df = pd.read_excel(os.path.join(_data_path, excel_file))

    def get_excel_data(self):
        """
        用pandas对excel里的数据进行获取
        :return: 返回值格式为列表里包含字典[{},{},{}.....]
        """
        try:
            _data = []
            for _i in self._df.index.values:
                # 用表格里每行遍历出来的值用字典的方式保存在row_data中
                _row_data = self._df.loc[_i].to_dict()
                _data.append(_row_data)
            return _data

        except FileNotFoundError:
            print('读取数据的文件不存在,请检查文件路径！')
            return False

        except Exception as e:
            print(f'读取数据失败 -> 错误原因 : {e}')
            return False


if __name__ == '__main__':
    # test = ExcelData('logindata.xlsx')
    # print(test.get_excel_data(['username', 'password']))
    # df = ExcelData('logindata.xlsx')
    # res = df.get_excel_data(['username', 'password'])
    # new = []
    # for i in res:
    #     res = [i['username'], i['password']]
    #     new.append(res)
    # print(new)
    pass
